package com.chery.media.music

import android.bluetooth.BluetoothAdapter
import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import com.blankj.utilcode.util.LogUtils

/**
 * 监听蓝牙设备状态
 */
class BluetoothBroadcastReceiver: BroadcastReceiver() {
    override fun onReceive(context: Context?, intent: Intent?) {
        LogUtils.eTag("蓝牙设备状态", "intent.action${intent?.action}")
        when(intent?.action) {
            BluetoothAdapter.ACTION_STATE_CHANGED -> {
                when(intent.getIntExtra(BluetoothAdapter.EXTRA_STATE, 0)) {
                    BluetoothAdapter.STATE_ON -> {
                        LogUtils.eTag("蓝牙设备状态", "打开")
                    }
                    BluetoothAdapter.STATE_OFF -> {
                        LogUtils.eTag("蓝牙设备状态", "关闭")
                    }
                }
            }
            BluetoothAdapter.ACTION_CONNECTION_STATE_CHANGED -> {
                when(intent.getIntExtra(BluetoothAdapter.EXTRA_CONNECTION_STATE, 0)) {
                    BluetoothAdapter.STATE_CONNECTED -> {
                        LogUtils.eTag("蓝牙设备状态", "已连接")
                    }
                    BluetoothAdapter.STATE_DISCONNECTED -> {
                        LogUtils.eTag("蓝牙设备状态", "断开连接")
                    }
                }
            }
        }
    }
}